package btcbot.entities;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * TODO Comment
 *
 * @author mollagnier
 */
public class Depth {

    private List<DepthEntry> bidList;
    private List<DepthEntry> askList;
    private Double minPrice;
    private Double maxPrice;

    public Depth(JSONObject jsonDepth) {
        super();
        this.minPrice = jsonDepth.getJSONObject("filter_min_price").getDouble("value");
        this.maxPrice = jsonDepth.getJSONObject("filter_max_price").getDouble("value");
        this.bidList = new ArrayList<DepthEntry>();
        JSONArray jsonBids = jsonDepth.getJSONArray("bids");
        for (int i = 0; i < jsonBids.size(); i++) {
            this.bidList.add(0, new DepthEntry(jsonBids.getJSONObject(i)));
        }
        this.askList = new ArrayList<DepthEntry>();
        JSONArray jsonAsks = jsonDepth.getJSONArray("asks");
        for (int i = 0; i < jsonAsks.size(); i++) {
            this.askList.add(new DepthEntry(jsonAsks.getJSONObject(i)));
        }
    }

    public List<DepthEntry> getBidList() {
        return bidList;
    }

    public void setBidList(List<DepthEntry> bidList) {
        this.bidList = bidList;
    }

    public List<DepthEntry> getAskList() {
        return askList;
    }

    public void setAskList(List<DepthEntry> askList) {
        this.askList = askList;
    }

    public Double getMinPrice() {
        return minPrice;
    }

    public void setMinPrice(Double minPrice) {
        this.minPrice = minPrice;
    }

    public Double getMaxPrice() {
        return maxPrice;
    }

    public void setMaxPrice(Double maxPrice) {
        this.maxPrice = maxPrice;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((askList == null) ? 0 : askList.hashCode());
        result = prime * result + ((bidList == null) ? 0 : bidList.hashCode());
        result = prime * result + ((maxPrice == null) ? 0 : maxPrice.hashCode());
        result = prime * result + ((minPrice == null) ? 0 : minPrice.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Depth other = (Depth) obj;
        if (askList == null) {
            if (other.askList != null)
                return false;
        }
        else if (!askList.equals(other.askList))
            return false;
        if (bidList == null) {
            if (other.bidList != null)
                return false;
        }
        else if (!bidList.equals(other.bidList))
            return false;
        if (maxPrice == null) {
            if (other.maxPrice != null)
                return false;
        }
        else if (!maxPrice.equals(other.maxPrice))
            return false;
        if (minPrice == null) {
            if (other.minPrice != null)
                return false;
        }
        else if (!minPrice.equals(other.minPrice))
            return false;
        return true;
    }

    @Override
    public String toString() {
        String string = "Depth [minPrice=" + minPrice + ", maxPrice=" + maxPrice + ", depth=";
        for (int i = 0; i < Math.max(bidList.size(), askList.size()); i++) {
            string += "\n";
            string += "bid:(" + (i < bidList.size() ? bidList.get(i).toString() : "") + ") ";
            string += "ask:(" + (i < askList.size() ? askList.get(i).toString() : "") + ")";
        }
        string += "\n]";
        return string;
    }

}